using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Text;

namespace SqlToolkit.Oracle.Schema
{
    public class SqlDbTable : SqlBase
    {
        #region Construction
        internal SqlDbTable() { }
        internal SqlDbTable(OracleDB conn) {_conn = conn;}
        #endregion
        #region TABLE_NAME
        string table_name = string.Empty;
        public string TableName
        {
            get { return table_name; }
            //set{if(value != table_name) table_name = value;}
        }
        #endregion
        #region Columns

        SqlDbTableFieldCollection col = null;
        public SqlDbTableFieldCollection Columns
        {
            get
            {
                if (null == col)
                    col = SqlSchema.TableColumns(_conn, this.table_name);

                return col;
            }
        }

        public bool HasColumn(string name)
        {
            foreach (SqlDbTableField fld in Columns)
            {
                if (fld.ColumnName == name.ToUpper())
                {
                    return true;
                }
            }

            return false;
        }

        #endregion
        #region +Attach Columns
        internal static SqlDbTable Attach(SqlDbTable tbl, SqlDbTableFieldCollection columns)
        {
            tbl.col = columns;
            return tbl;

        }
        #endregion
        #region +Build
        internal static SqlDbTable Build(OracleDB conn, System.Data.IDataReader r)
        {
            SqlDbTable ret = new SqlDbTable(conn);

            if (r.IsDBNull(0))
                ret.table_name = "ERROR INVALID RESULT SET";
            else
                ret.table_name = r.GetString(0);

            return ret;
        }
        #endregion
        #region +BuildCollection
        internal static SqlDbTableCollection BuildCollection(OracleDB conn, System.Data.IDataReader r)
        {
            SqlDbTableCollection tbls = new SqlDbTableCollection();

            while (r.Read())
                tbls.Add(SqlDbTable.Build(conn, r));


            return tbls;
        }
        #endregion

        SqlDbConstraintCollection constraints = null;
        public SqlDbConstraintCollection Constraints
        {
            get
            {
                if (null == constraints)
                    constraints = SqlSchema.TableConstraints(_conn, this.table_name);

                return constraints;
            }
        }

        public string TableNameFriendly()
        {
            return FriendlyName(this.TableName);
        }

        //public DataTable GetData()
        //{
        //    DataTable ret = null;

        //    if (null != this._conn)
        //    {
        //        string sql = SqlGenerator.CreateQuery(this, null);
        //        try
        //        {
        //            ret = this._conn.DbAccessor.ExecuteSqlText.ExecuteDataTable(sql);
        //        }
        //        catch (Exception ex)
        //        {
        //            System.Diagnostics.Debug.WriteLine(ex.Message);
        //            ret = null;
        //        }
        //    }

        //    return ret;
        //}

        public int GetColumnIndex(string columnName)
        {
            if (null == this.Columns) { return -1; }

            for(int i = 0;i < this.Columns.Count; i++)
            {
                if(this.Columns[i].ColumnName.ToUpper().Trim() == columnName.ToUpper().Trim())
                {
                    return i;
                }
            }

            return -1;
        }

    }
}
